package com.screen.screendisplay.service.impl.electric;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.screen.screendisplay.entity.electric.ElectricOpermonPlant;
import com.screen.screendisplay.mapper.electric.ElectricOpermonPlantMapper;
import com.screen.screendisplay.service.electric.ElectricOpermonPlantService;
import com.screen.screendisplay.vo.electric.PlantCapacityStatistics;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 火电运行监控电厂表 服务实现类
 * </p>
 *
 * @author xuesong
 * @since 2025-09-24
 */
@Service
public class ElectricOpermonPlantServiceImpl extends ServiceImpl<ElectricOpermonPlantMapper, ElectricOpermonPlant> implements ElectricOpermonPlantService {

    @Override
    public List<PlantCapacityStatistics> getPlantCapacityStatistics(Integer year) {
        // 查询指定年份的所有数据
        QueryWrapper<ElectricOpermonPlant> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("year", year);
        queryWrapper.orderBy(true, false, "month"); // 按月份倒序排列
        List<ElectricOpermonPlant> plantList = list(queryWrapper);
        
        if (plantList.isEmpty()) {
            return new ArrayList<>();
        }
        
        // 按电厂编码分组，取每个电厂最新月份的数据
        Map<String, ElectricOpermonPlant> latestPlantDataMap = plantList.stream()
                .collect(Collectors.toMap(
                        ElectricOpermonPlant::getPlantCode,
                        plant -> plant,
                        (existing, replacement) -> {
                            // 如果月份更大，使用新的数据
                            if (replacement.getMonth() > existing.getMonth()) {
                                return replacement;
                            }
                            return existing;
                        }
                ));
        
        // 转换为VO对象
        List<PlantCapacityStatistics> resultList = new ArrayList<>();
        for (ElectricOpermonPlant plant : latestPlantDataMap.values()) {
            PlantCapacityStatistics statistics = new PlantCapacityStatistics();
            statistics.setPlantCode(plant.getPlantCode());
            statistics.setPlantName(plant.getPlantName());
            statistics.setTotalCapacity(plant.getTotalCapacity() != null ? plant.getTotalCapacity() : BigDecimal.ZERO);
            statistics.setOperatingCapacity(plant.getOperatingCapacity() != null ? plant.getOperatingCapacity() : BigDecimal.ZERO);
            statistics.setOutageCapacity(plant.getOutageCapacity() != null ? plant.getOutageCapacity() : BigDecimal.ZERO);
            statistics.setOperationRate(plant.getOperationRate() != null ? plant.getOperationRate() : BigDecimal.ZERO);
            resultList.add(statistics);
        }
        
        // 按电厂编码排序
        resultList.sort((a, b) -> a.getPlantCode().compareTo(b.getPlantCode()));
        
        return resultList;
    }

}
